// 添加灭火器信息
export function insertInfo(info) {
	console.log('添加灭火器信息: ');
	return new Promise((resolve, reject) => {
		plus.sqlite.executeSql({
			name: 'fire',
			sql: 'create table if not exists info("id" CHAR(20) PRIMARY KEY,"location" CHAR(100),"status" CHAR(20),"other" CHAR(100),"time" CHAR(100),"check" CHAR(10))',
			success: function() {
				console.log('新建灭火器信息表!');
				plus.sqlite.executeSql({
					name: 'fire',
					sql: 'insert into info values("' + info.id + '","' + info.location + '","' + info.status + '","' + info.other +
						'","' + info.time + '","' + info.check + '")',
					success: function() {
						resolve('executeSql success!');
						console.log('添加灭火器信息成功!');
					},
					fail: function(e) {
						reject(e);
						console.log('添加灭火器信息失败!' + JSON.stringify(e));
					}
				})
			},
			fail: function(e) {
				reject(e);
				console.log('新建灭火器信息表失败!' + JSON.stringify(e));
			}
		});
	})
};

// 删除灭火器信息
export function removeInfo(info) {
	console.log('删除灭火器信息!');
	plus.sqlite.executeSql({
		name: 'fire',
		sql: 'delete from info where id = ' + '"' + info.id + '"',
		success: function(e) {
			console.log('删除灭火器信息成功!');
		},
		fail: function(e) {
			console.log('删除灭火器信息失败!' + JSON.stringify(e));
		}
	});
};

// 清空所有灭火器数据
export function removeAllInfo() {
	console.log('清空数据!');
	plus.sqlite.executeSql({
		name: 'fire',
		sql: 'delete from info',
		success: function(e) {
			console.log('清空数据成功!');
		},
		fail: function(e) {
			console.log('清空数据失败!' + JSON.stringify(e));
		}
	});
};

// 修改灭火器信息
export function updateInfo(info) {
	console.log('修改灭火器信息');
	plus.sqlite.executeSql({
		name: 'fire',
		sql: 'update info set location = "' + info.location + '",status = "' + info.status + '",other = "' + info.other + '",`check` = "' + info.check + '",time = "' + info.time + 
			'" where id = ' + '"' + info.id + '"',
		success: function(e) {
			console.log('修改灭火器信息成功!');
		},
		fail: function(e) {
			console.log('修改灭火器信息失败!' + JSON.stringify(e));
		}
	});
};

// 开始检查灭火器
export function beginCheck() {
	console.log('开始检查灭火器!');
	plus.sqlite.executeSql({
		name: 'fire',
		sql: 'update info set `check` = "待检查"',
		success: function(e) {
			console.log('开始检查灭火器就绪!');
		},
		fail: function(e) {
			console.log('开始检查灭火器失败!' + JSON.stringify(e));
		}
	});
};

// 结束检查灭火器
export function endCheck() {
	console.log('开始检查灭火器!');
	plus.sqlite.executeSql({
		name: 'fire',
		sql: 'update info set `check` = "已检查"',
		success: function(e) {
			console.log('开始检查灭火器就绪!');
		},
		fail: function(e) {
			console.log('开始检查灭火器失败!' + JSON.stringify(e));
		}
	});
};

// 查询灭火器信息
export function selectInfo() {
	console.log('查询灭火器信息!');
	return new Promise((resolve, reject) => {
		plus.sqlite.selectSql({
			name: 'fire',
			sql: 'select * from info',
			success: function(data) {
				resolve(data);
				console.log('查询灭火器信息成功!');
			},
			fail: function(e) {
				reject('selectSql fail: ' + JSON.stringify(e));
				console.log('查询灭火器信息失败!');
			}
		});
	});
};

// 根据编号查询灭火器信息
export function selectInfoById(id) {
	console.log('查询灭火器信息!');
	return new Promise((resolve, reject) => {
		plus.sqlite.selectSql({
			name: 'fire',
			sql: 'select * from info where id = "' + id + '"',
			success: function(data) {
				resolve(data);
				console.log('查询灭火器信息成功!');
			},
			fail: function(e) {
				reject('selectSql fail: ' + JSON.stringify(e));
				console.log('查询灭火器信息失败!');
			}
		});
	});
};
